package com.sanjiahantang.ythshopping.activity.me;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.adapter.EvaluateListAdapter;
import com.sanjiahantang.ythshopping.model.EvaluateModle;
import com.sanjiahantang.ythshopping.presenter.EvaluateListPresenter;
import com.sanjiahantang.ythshopping.views.EvaluateView;

public class EvaluateListActivity extends Activity implements EvaluateView, View.OnClickListener, AbsListView.OnScrollListener {

    ListView lv_evaluate;
    EvaluateListAdapter evaadapter;
    TextView tv_all, tv_wait, tv_finish, tv_title;
    EvaluateListPresenter evaluateListPresenter;
    ImageView iv_back;

    boolean scroll = false;

    int page = 1;
    String type = "all";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluete_list);

    }

    public void InitView() {
        lv_evaluate = (ListView) findViewById(R.id.lv_evalist);
        tv_all = (TextView) findViewById(R.id.tv_evalist_all);
        tv_finish = (TextView) findViewById(R.id.tv_evalist_finish);
        tv_wait = (TextView) findViewById(R.id.tv_evalist_wait);
        tv_title = (TextView) findViewById(R.id.title);
        iv_back = (ImageView) findViewById(R.id.back);

        evaadapter = new EvaluateListAdapter(this, R.layout.evaluate_item);
        lv_evaluate.setAdapter(evaadapter);
        tv_wait.setOnClickListener(this);
        tv_finish.setOnClickListener(this);
        tv_all.setOnClickListener(this);
        tv_title.setText("我的评价");
        iv_back.setOnClickListener(this);

        evaluateListPresenter = new EvaluateListPresenter(EvaluateListActivity.this, this);
        evaluateListPresenter.LoadEvaluateList("all", page);
        lv_evaluate.setOnScrollListener(this);
    }

    @Override
    public void getEvaluateList(EvaluateModle model) {
        if (model.getData() != null) {
            evaadapter.addAll(model.getData());
            scroll = true;
        } else {
            scroll = false;
            Toast.makeText(this, "没有更多订单啦", Toast.LENGTH_LONG).show();
        }


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_evalist_all:
                evaadapter.clear();
                type = "all";
                page = 1;
                scroll = false;
                scoll = false;
                evaluateListPresenter.LoadEvaluateList(type, page);
                setStatic(true, false, false);
                break;
            case R.id.tv_evalist_finish:
                evaadapter.clear();
                type = "finish";
                page = 1;
                scroll = false;
                scoll = false;
                evaluateListPresenter.LoadEvaluateList(type, page);
                setStatic(false, false, true);
                break;
            case R.id.tv_evalist_wait:
                evaadapter.clear();
                type = "wait";
                page = 1;
                scroll = false;
                scoll = false;
                evaluateListPresenter.LoadEvaluateList(type, page);
                setStatic(false, true, false);
                break;
            case R.id.back:
                finish();
                break;

        }

    }

    public void setStatic(boolean all, boolean wait, boolean finish) {
        if (all) {
            tv_all.setTextColor(getResources().getColor(R.color.white));
            tv_all.setBackgroundColor(getResources().getColor(R.color.prospect));
        } else {
            tv_all.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_all.setBackgroundColor(getResources().getColor(R.color.white));
        }
        if (wait) {
            tv_wait.setTextColor(getResources().getColor(R.color.white));
            tv_wait.setBackgroundColor(getResources().getColor(R.color.prospect));
        } else {
            tv_wait.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_wait.setBackgroundColor(getResources().getColor(R.color.white));
        }
        if (finish) {
            tv_finish.setTextColor(getResources().getColor(R.color.white));
            tv_finish.setBackgroundColor(getResources().getColor(R.color.prospect));
        } else {
            tv_finish.setTextColor(getResources().getColor(R.color.text_gary_color));
            tv_finish.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }

    Boolean scoll = false;

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        scoll = true;


    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if ((firstVisibleItem + visibleItemCount) >= totalItemCount && scroll && scoll) {
            scroll = false;
            page++;
            evaluateListPresenter.LoadEvaluateList(type, page);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        scroll = false;

        page = 1;
        InitView();
    }
}
